package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Advertisement;
import com.wf.smartcomm.server.bean.Complain;
import com.wf.smartcomm.server.bean.Guide;
import com.wf.smartcomm.server.bean.Praise;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.bean.Shop;
import com.wf.smartcomm.server.bean.ThumbImage;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.service.ThumbImageService;
import com.wf.smartcomm.server.util.IdUtils;

@Service
@Transactional
public class ThumbImageServiceImpl implements ThumbImageService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(ThumbImage thumbImage) {
		thumbImage.setObjectId(IdUtils.uuid());
		thumbImage.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(thumbImage);
	}

	@Override
	public void save(List<ThumbImage> images) {
		for (ThumbImage thumbImage : images) {
			thumbImage.setObjectId(IdUtils.uuid());
			thumbImage.setCreatedAt(String.valueOf(System.currentTimeMillis()));
			factory.getCurrentSession().persist(thumbImage);
		}
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByComplain(Complain complain) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where complain = :complain");
		query.setParameter("praise", complain);
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByPraise(Praise praise) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where praise = :praise");
		query.setParameter("praise", praise);
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByRepair(Repair repair) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where repair = :repair");
		query.setParameter("repair", repair);
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByGuide(Guide guide) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where guide = :guide");
		query.setParameter("guide", guide);
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByShop(Shop shop) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where shop = :shop");
		query.setParameter("shop", shop);
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByVillage(Village village) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where village = :village");
		query.setParameter("village", village);
		return query.list();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ThumbImage> listByAdvertisement(Advertisement ad) {
		Query query = factory.getCurrentSession().createQuery("from ThumbImage where ad = :ad");
		query.setParameter("ad", ad);
		return query.list();
	}

	@Override
	public void deleteGuideImages(String objectId) {
		String sql = "delete from t_thumbImage where guideId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", objectId);
		query.executeUpdate();
	}

	@Override
	public void deleteAdImages(String objectId) {
		String sql = "delete from t_thumbImage where adId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", objectId);
		query.executeUpdate();
	}

	@Override
	public void deleteAnnounceImages(String objectId) {
		String sql = "delete from t_thumbImage where anId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", objectId);
		query.executeUpdate();
	}

	@Override
	public void deleteShopImages(String objectId) {
		String sql = "delete from t_thumbImage where shopId =:id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", objectId);
		query.executeUpdate();
	}

}
